package com.feihong.yw189.bluetooth.startwritecard;

import android.app.Activity;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.bluetooth.listener.SetDialogListener;

/**
 * 补卡/写卡
 * 张鹏
 */
public class WriteCardDialog extends Dialog implements View.OnClickListener {

    private BluetoothAdapter ba;
    private SetDialogListener mOnSetDialogListener;
    private SearchInterface searchListener;
    private String type;

    /**
     * @param context              上下文
     * @param type                 读卡/写卡(固定字符串)
     * @param orderSn              订单号
     * @param token
     * @param searchListener
     * @param mOnSetDialogListener
     */
    public WriteCardDialog(Activity context, String type, String orderSn, String token,
                           SearchInterface searchListener,
                           SetDialogListener mOnSetDialogListener) {
        super(context, R.style.BtDialogStyle);
        this.mContext = context;
        this.type = type;
        this.orderSn = orderSn;
        this.token = token;
        this.searchListener = searchListener;
        this.mOnSetDialogListener = mOnSetDialogListener;
    }

    public interface SearchInterface {
        void searchBt(BluetoothAdapter ba, String orderSn, int index, String str, String token); // 搜索蓝牙
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bt_name_dialog);
        initView();
        initData();
    }

    /**
     * 获取蓝牙
     */
    private void initData() {

        // 判断蓝牙是否开启
        ba = BluetoothAdapter.getDefaultAdapter();
        if (!ba.isEnabled()) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            mContext.startActivity(intent);
        }
    }

    private int index = 0;
    private String str = "";

    @Override
    public void onClick(View view) {


        if (view.getId() == R.id.btn_dismess) {
            dismiss();
        } else {
            switch (view.getId()) {
                case R.id.btn_kr: // KT8000
                    index = 0;
                    str = "KT8000";
                    break;
                case R.id.btn_krb: // KT8003
                    index = 1;
                    str = "KT8003";
                    break;
                case R.id.btn_sr: // 森瑞
                    index = 2;
                    str = "SR";
                    break;
                case R.id.btn_hhd: // 恒鸿达
                    index = 3;
                    str = "HOD";
                    break;
                case R.id.btn_djlm: // 店家联盟
                    index = 4;
                    str = "DJLM";
                    break;
            }
            searchListener.searchBt(ba, orderSn, index, str, token);
            dismiss();
        }
    }


    //*******************************初始化**************************************
    private void initView() {
        btnHs = (TextView) findViewById(R.id.btn_hs);
        btnSs = (TextView) findViewById(R.id.btn_ss);
        btnNfc = (TextView) findViewById(R.id.btn_nfc);

        btnKr = (TextView) findViewById(R.id.btn_kr);
        btnKrb = (TextView) findViewById(R.id.btn_krb);
        btnSr = (TextView) findViewById(R.id.btn_sr);
        btnHhd = (TextView) findViewById(R.id.btn_hhd);
        btnDjlm = (TextView) findViewById(R.id.btn_djlm);

        btnDismess = (TextView) findViewById(R.id.btn_dismess);

        btnKr.setText("KT8000(蓝牙)" + type);
        btnKrb.setText("KT8003(蓝牙)" + type);
        btnSr.setText("森锐(蓝牙)" + type);
        btnHhd.setText("恒鸿达(蓝牙)" + type);
        btnDjlm.setText("店家联盟(蓝牙)" + type);

        btnHs.setVisibility(View.GONE);
        btnSs.setVisibility(View.GONE);
        btnNfc.setVisibility(View.GONE);

        btnKr.setOnClickListener(this);
        btnKrb.setOnClickListener(this);
        btnSr.setOnClickListener(this);
        btnHhd.setOnClickListener(this);
        btnDjlm.setOnClickListener(this);
        btnDismess.setOnClickListener(this);
    }


    //*********************************全局变量********************************************
    private TextView btnHs;
    private TextView btnKr;
    private TextView btnKrb;
    private TextView btnSs;
    private TextView btnSr;
    private TextView btnHhd;
    private TextView btnDjlm;
    private TextView btnNfc;
    private TextView btnDismess;
    private Activity mContext;
    private String orderSn;
    private String token;

}
